package com.catbad.controller;

import com.catbad.annotations.Auth;
import com.catbad.domain.Area;
import com.catbad.domain.Cinema;
import com.catbad.services.AreaServices;
import com.catbad.services.CinemaServices;
import com.catbad.vo.AreaVO;
import com.catbad.vo.CinemaPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class AdminCinemaController {
    @Autowired
    private AreaServices areaServices;
    @Autowired
    private CinemaServices cinemaServices;

    /**
     * 进入影院管理页面
     * @return
     */
    @Auth("com.catbad.admin.cinema")
    @RequestMapping("admincinema")
    public String adminCinema(Integer aid1, Integer aid2, String cname, Integer pageNO, Integer rows, Model model){
        //得到页面相关数据
        CinemaPageVO cinemaPageVO = cinemaServices.findPageForCinema(aid1,aid2,cname,pageNO,rows);
        model.addAttribute("cinemaPageVO",cinemaPageVO);
        return"admin/cinema";
    }

    /**
     * 添加影院
     * @param cinema
     * @return
     */
    @Auth("com.catbad.admin.cinema")
    @RequestMapping("savecinema")
    @ResponseBody
    public String savecinema(Cinema cinema){
        int code = cinemaServices.saveCinema(cinema);
        if(code==1){return "1";}
        if(code==2){return "2";}
        if(code==3){return "3";}
        if(code==4){return "4";}
        return "0";
    }

    /**
     * 根据一级地址，返回对应的二级地址
     * @param pid
     * @return
     */
    @RequestMapping("area2")
    @ResponseBody
    public AreaVO getArea2(Integer pid){
        AreaVO areaVO = new AreaVO();
        List<Area> areaList = areaServices.selectArea2(pid);
        if(areaList!=null) {
            //有数据
            areaVO.setData(areaList);
            areaVO.setCode(0);
        }else {
            //无数据
            areaVO.setCode(1);
        }
        return areaVO;
    }

    /**
     * 影院删除
     * @param cid
     * @return
     */
    @Auth("com.catbad.admin.cinema")
    @RequestMapping("cinemaDelete")
    @ResponseBody
    public String cinemaDelete(Long cid){
        int code = cinemaServices.deleteCinema(cid);
        if(code==1){return "1";}
        if(code==0){return "0";}
        return "-1";
    }
    /**
     * 影院编辑页面初始化
     * @param cid
     * @return
     */
    @Auth("com.catbad.admin.cinema")
    @RequestMapping("setForCinema")
    @ResponseBody
    public Map<String,Object> setForCinema(Long cid){
        return cinemaServices.selectForSet(cid);
    }

    /**
     * 修改影院信息
     * @param cinema
     * @return
     */
    @Auth("com.catbad.admin.cinema")
    @RequestMapping("setCinema")
    @ResponseBody
    public String setCinema(Cinema cinema){
        int code = cinemaServices.setCinema(cinema);
        if(code==1){return "1";}
        return "0";
    }
}
